package com.macro.mall.portal.controller;


import com.macro.mall.common.api.CommonResult;
import com.macro.mall.mgb.model.Member;
import com.macro.mall.portal.service.MemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangwang
 * @version 1.0
 * @description 会员管理
 * @date 2025/7/17 16:40
 */
@Slf4j
@RestController
@RequestMapping("/sso")
@Tag(name = "UmsMemberController",description = "会员管理接口")
public class MemberController {

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    @Autowired
    private MemberService memberService;


    @Operation(summary = "会员注册")
    @PostMapping("/register")
    public CommonResult register(@RequestParam String username,
                                 @RequestParam String password,
                                 @RequestParam String telephone,
                                 @RequestParam String authCode) {
        memberService.register(username, password, telephone, authCode);
        return CommonResult.success(null,"注册成功");
    }

    @GetMapping("/getAuthCode")
    @Operation(summary = "获取验证码")
    public CommonResult getAuthCode(@RequestParam String telephone){
        String authCode = memberService.getAuthCode(telephone);
        return CommonResult.success(authCode,"获取验证码成功");
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public CommonResult login(@RequestParam String username,
                              @RequestParam String password){
        log.info("开始登录。。。");
        String token = memberService.login(username,password);
        if(token == null){
            return CommonResult.validateFailed("用户名或密码错误");
        }
        log.info("token:{}",token);
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        tokenMap.put("tokenHead",tokenHead);
        return CommonResult.success(tokenMap);
    }

    @Operation(summary = "获取会员信息")
    @GetMapping("/info")
    public CommonResult info(Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        Member member = memberService.getCurrentMember();
        return CommonResult.success(member);
    }

    @Operation(summary = "刷新token")
    @GetMapping("/refreshToken")
    public CommonResult refreshToken(HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        String refreshToken = memberService.refreshToken(token);
        if (refreshToken == null) {
            return CommonResult.failed("token已经过期！");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", refreshToken);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }
}


